/**
 * Created by DE1572077 on 18.04.14.
 */
//App Router

var websiteUrl = "file:///D:/Dokumente/Development/Mylife/WebContent/index.html";

Backbone.history.start();

var AppRouter = Backbone.Router.extend({
    routes:{
        'login':'login',
        'default':'default'
    }
});

var appRouter = new AppRouter;

appRouter.on('route:login', function(actions){
    console.log('Route:login');
    console.log('Route:login User.sessionId:' + MyApp.user.get('sessionId'))

    //Login Sequence
    MyApp.header.close();
    MyApp.sidebar.close();
    var sessionId = readCookie('sessionId');

    if(sessionId === ''){
        sessionId = 'nosessionid';
        console.log("router:No Cookie found.");
        MyApp.module("LoginModule").start(MyApp.main);
    }else{
        console.log("router:Cookie found.");
        MyApp.module('LoginModule').trigger('LoginView:login',"-1","-1", sessionId);
    }

});

appRouter.on('route:default', function(actions){

    console.log('Route:default');
    console.log('Route:default sessionId:' + MyApp.user.get('sessionId'))

    if(true){
        MyApp.header.show(MyApp.navBarView);
        MyApp.sidebar.show(MyApp.sideBarView);
        MyApp.module("NewEntryModule").trigger('start', MyApp.main);
    }else{
        console.log("route:default redirecting to login");
        document.location.href = websiteUrl + "#/login";
    }

});